package com.ligq.vbasedemo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.ligq.vbasedemo.fragment.CameraPreviewFragment;
import com.ligq.vbasedemo.fragment.H264DecodeFragment;
import com.ligq.vbasedemo.fragment.ScreenRecordingFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "ligq";
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();
        fragments = new ArrayList<>();
        fragments.add(new H264DecodeFragment());
        fragments.add(new ScreenRecordingFragment());
        fragments.add(new CameraPreviewFragment());
        showFragment(R.id.container, 0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.func, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.h264_decode:
                showFragment(R.id.container, 0);
                return true;
            case R.id.screen_record:
                showFragment(R.id.container, 1);
                return true;
            case R.id.camera:
                showFragment(R.id.container, 2);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }

    }

    private void showFragment(int destId, int index) {
        getSupportFragmentManager().beginTransaction().replace(destId, fragments.get(index)).commit();
    }

    public void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
            }, 1);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "onRequestPermissionsResult: " + permissions[i] + " get permission failed!!");
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


}